package com.modifiertech.lineage.network;

import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.handler.SkillHandingOffHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import java.util.ArrayList;
import java.util.List;

/**客户端通知服务端
 *
 * 技能范围选定状态更新
 *
 * @description: default
 * @author: saliencemn
 * @mail: hackingmu@outlook.com
 * @date: 2024/9/14 23:29
 */
public class MessageServerSkillRange implements IMessage, IMessageHandler<MessageServerSkillRange,IMessage> {
    private NBTTagCompound nbtTagCompound=new NBTTagCompound();

    @Override
    public void fromBytes(ByteBuf byteBuf) {
        nbtTagCompound= ByteBufUtils.readTag(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf) {
        ByteBufUtils.writeTag(byteBuf,nbtTagCompound);
    }

    @Override
    public IMessage onMessage(MessageServerSkillRange messageClientSkillRange, MessageContext messageContext) {
        //收到范围选定状态更新通知消息后,覆盖到客户端playerData 中
        FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(()->{
            Lineage.proxy.handleMessageServerSkillRange(messageClientSkillRange, messageContext,()->{
                //发送服务端事件
                SkillHandingOffHandler.INSTANCE.skillHandingOff(messageContext.getServerHandler().player
                        ,readSkillName(messageClientSkillRange.nbtTagCompound));
                return null;
            });
        });
        return null;
    }

    public synchronized MessageServerSkillRange write(Boolean state,List<AxisAlignedBB> alignedBBs,String skillName){
        //INIT
        this.getNbtTagCompound().setString("skillName",skillName);
        if (null!= state){
            this.getNbtTagCompound().setBoolean("state",state);
        }
        if (null!= alignedBBs){
            this.getNbtTagCompound().setTag("AlignedBBs", transAABBRange(alignedBBs));
        }
        return this;
    }


    //从 NBTTagCompound 中 解析 List<AxisAlignedBB>
    public static List<AxisAlignedBB> readAABBRange(NBTTagCompound compound){
        List<AxisAlignedBB> alignedBBs = new ArrayList<>();

        // 获取 NBTTagList
        NBTTagList nbtList = compound.getTagList("AlignedBBs", 10);
        // 10 是 NBTTagCompound 类型的 ID

        // 遍历 NBTTagList 中的每个 NBTTagCompound
        for (int i = 0; i < nbtList.tagCount(); i++) {
            NBTTagCompound boxNBT = nbtList.getCompoundTagAt(i);

            // 读取每个 AxisAlignedBB 的坐标
            double minX = boxNBT.getDouble("minX");
            double minY = boxNBT.getDouble("minY");
            double minZ = boxNBT.getDouble("minZ");
            double maxX = boxNBT.getDouble("maxX");
            double maxY = boxNBT.getDouble("maxY");
            double maxZ = boxNBT.getDouble("maxZ");

            // 创建 AxisAlignedBB 对象并添加到列表中
            alignedBBs.add(new AxisAlignedBB(minX, minY, minZ, maxX, maxY, maxZ));
        }
        return alignedBBs;
    }
    // 从 List<AxisAlignedBB> 转换 NBTTagList
    public static NBTTagList transAABBRange(List<AxisAlignedBB> alignedBBs){
        NBTTagList nbtList = new NBTTagList();
        // 遍历 List 中的每个 AxisAlignedBB
        for (AxisAlignedBB box : alignedBBs) {
            // 创建一个 NBTTagCompound 来保存这个 AxisAlignedBB 的数据
            NBTTagCompound boxNBT = new NBTTagCompound();
            boxNBT.setDouble("minX", box.minX);
            boxNBT.setDouble("minY", box.minY);
            boxNBT.setDouble("minZ", box.minZ);
            boxNBT.setDouble("maxX", box.maxX);
            boxNBT.setDouble("maxY", box.maxY);
            boxNBT.setDouble("maxZ", box.maxZ);
            // 将这个 NBTTagCompound 添加到 NBTTagList 中
            nbtList.appendTag(boxNBT);
        }
        return nbtList;
    }
    //从 NBTTagCompound 中 解析 SkillName
    public String readSkillName(NBTTagCompound compound){
        return compound.getString("skillName");
    }





    public NBTTagCompound getNbtTagCompound() {
        return nbtTagCompound;
    }

    public void setNbtTagCompound(NBTTagCompound nbtTagCompound) {
        this.nbtTagCompound = nbtTagCompound;
    }
}
